﻿using Bystd.Geo.Geometries;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bystd.Geo.GeoParsers.GeoJson
{
    public class GeoJsonMultiLineString<T, MLine>
          : GeoJsonObject<T, MLine> where MLine : LineMArray
    {
        public GeoJsonMultiLineString() { }
        public GeoJsonMultiLineString(Feature<T, MLine>[] features)
         : base()
        {
            this.features = features;
        }
    }

    public class GeoJsonMultiLineString : GeoJsonGeometry
    {
        public List<List<double[]>> coordinates { get; set; }

        public GeoJsonMultiLineString() { }

        public GeoJsonMultiLineString(string jsonString)
        {
            var geo = jsonConvert.ConvertTo<GeoJsonMultiLineString>(jsonString);
            this.coordinates = geo.coordinates;
            this.type = geo.type;
        }

        public GeoJsonMultiLineString(Geometry geometry)
        {
            this.Geometry = geometry;
        }

        public Geometry ToGeometry()
        {
            List<GeoSequence> seqs = new List<GeoSequence>(coordinates.Count);

            for (int i = 0; i < coordinates.Count; ++i)
            {
                var itemArray = coordinates[i];
                Coordinate[] coords = new Coordinate[itemArray.Count];

                for (int j = 0; j < coords.Length; ++j)
                {
                    coords[j] = new Coordinate()
                    {
                        X = itemArray[j][0],
                        Y = itemArray[j][1],
                        Z = itemArray[j].Length >= 3 ? itemArray[j][2] : 0
                    };
                }
                seqs.Add(new GeoSequence(coords));
            }
            var gtype = GeometryType;
            var geo = new Geometry(gtype, seqs);
            return geo;
        }

        public string FromGeometry()
        {
            this.type = getType();

            var lines = Geometry.GetGemoetry(0);
            coordinates = new List<List<double[]>>(lines.Count);

            foreach (var line in lines)
            {
                List<double[]> _line = new List<double[]>(line.Count);
                for (int i = 0; i < line.Count; ++i)
                {
                    _line.Add(new double[] { line[i].X, line[i].Y });
                }
                coordinates.Add(_line);
            }

            return jsonConvert.ConvertFrom(this);
        }
    }

}
